package com.demo.testui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

public class MyViewGroup extends FrameLayout {
    private final String TAG = Tags.myViewGroup;


    //常量
    private final int DEFAULT_HEIGHT = DensityUtil.dip2px(getContext(), 200);//设置默认的高
    private final int DEFAULT_WIDTH = DensityUtil.dip2px(getContext(), 100);//设置默认的宽

    //变量
    private int parentWidth = 0;//控件的宽
    private int parentHeight = 0;//控件的高

    public static float percent = Constant.PERCENT_DEFAULT;

    public MyViewGroup(Context context) {
        this(context, null);
    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        setWillNotDraw(false);
    }

    /**
     * 测量-->设置大小
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.d(TAG, "begin widthMeasureSpec=" + MeasureSpec.toString(widthMeasureSpec) + " heightMeasureSpec=" + MeasureSpec.toString(heightMeasureSpec));

        //获取测量模式
        int width_mode = MeasureSpec.getMode(widthMeasureSpec);
        int height_mode = MeasureSpec.getMode(heightMeasureSpec);

        //获取测量值
        int width_size = MeasureSpec.getSize(widthMeasureSpec);
        int height_size = MeasureSpec.getSize(heightMeasureSpec);

        //根据测量结果设置最终的宽度和高度
        parentWidth = (int) (opinionWidthOrHeight(width_mode, width_size, DEFAULT_WIDTH) * percent);//宽度的测量结果
        parentHeight = (int) (opinionWidthOrHeight(height_mode, height_size, DEFAULT_HEIGHT) * percent);//高度的测量结果

        //设置子控件
        setChildLayoutParams();
        setMeasuredDimension(parentWidth, parentHeight);

        Log.d(TAG, "last parentWidth=" + parentWidth + " parentHeight=" + parentHeight);
    }

    /**
     * 设置子控件的大小
     */
    private void setChildLayoutParams() {
        //获取子控件的个数
        int childCounts = getChildCount();
        //设置子控件的大小
        for (int i = 0; i < childCounts; i++) {
            View childView = getChildAt(i);
            FrameLayout.LayoutParams params;
            if (i == 1) {
                params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
            } else {
                params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            }
            childView.setLayoutParams(params);
        }
    }

    /**
     * 根据模式判断宽度或者高度
     *
     * @param mMode
     * @param mSize
     * @param mDefault
     * @return
     */
    private int opinionWidthOrHeight(int mMode, int mSize, int mDefault) {
        int result = 0;
        if (mMode == MeasureSpec.EXACTLY) {
            result = mSize;
            Log.d(TAG, "确定模式，返回：" + result);

        } else {
            // 设置默认宽度
            if (mMode == MeasureSpec.AT_MOST) {
                result = Math.min(mSize, mDefault);
                Log.d(TAG, "非确定模式，返回min(默认值=" + mDefault + ",建议值=" + mSize + ")：" + result);
            }
        }
        return result;
    }

}
